/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.helpers
{
	import flash.events.Event;
	import flash.events.IEventDispatcher;

	internal class ContextHelper implements IHelper {
//		private static var _pool:Array = new Array();
		private var _state:HelperState;		
		private var _context:IContextHandler;
		private var _initialState:InitialState;
		private var _loopState:LoopState;
		private var _pauseState:PauseState;
		
		public function ContextHelper() {
			_initialState = new InitialState();
			_loopState = new LoopState;
			_pauseState = new PauseState;
		}
		
		private function initialize(handler:IContextHandler,immediately:Boolean=true):void {
			_context = handler;
			context.addEventListener(HelperEvent.DONE, cleanUpHandler);
			context.addEventListener(HelperEvent.CANCELED, cleanUpHandler);
			context.addEventListener(HelperEvent.SKIPPED, cleanUpHandler);

			_initialState.initialize(handler,_initialState,_loopState,_pauseState);
			_loopState.initialize(handler,_initialState,_loopState,_pauseState);
			_pauseState.initialize(handler,_initialState,_loopState,_pauseState);
			
			state = _initialState;
			if (immediately) {
				start();
			}
		}
		
		public static function getInstance(handler:IContextHandler,immediately:Boolean=true):IHelper {
			var helper:ContextHelper;
//			if (_pool.length == 0) {
				helper = new ContextHelper();
//			} else {
//				helper = _pool.shift();
//			}
			helper.initialize(handler,immediately);
			return helper;
		}
		
		public function start():void {
			if (state==null) {
				return;
			}
			state = state.start();
		}
		
		public function pause():void {
			if(state==null) {
				return;
			}
			state = state.pause();
		}
		
		public function cancel():void {
			if (state==null) {
				return;
			}
			state = state.cancel();
		}
		
		public function resume():void {
			if(state==null) {
				return;
			}
			state = state.resume();
		}
		
		public function revert():void {
			if (state == null) {
				return;
			}
			state = state.revert();
		}
		
		public function skip():void {
			if (state == null) {
				return;
			}
			state = state.skip();
		}
		
		private function get context():IEventDispatcher {
			return _context as IEventDispatcher;
		}
		
		private function cleanUpHandler(event:Event):void {
			context.removeEventListener(HelperEvent.DONE, cleanUpHandler);
			context.removeEventListener(HelperEvent.CANCELED, cleanUpHandler);
			context.removeEventListener(HelperEvent.SKIPPED, cleanUpHandler);
			state = state.cleanUp();
			_context = null;
//			_pool.push(this);
		}
		
		private function set state(value:HelperState):void {
			_state = value;
		}
		
		private function get state():HelperState {
			return _state;
		}
	}
}